// Copyright (C) 2021 Focus Media Holding Ltd. All Rights Reserved.

package cn.focusmedia.central.${appCode}.app.service;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.focusmedia.central.${appCode}.app.convert.${className}DTOConvert;
import cn.focusmedia.central.${appCode}.client.dto.${modelPackName}.${className}AddDTO;
import cn.focusmedia.central.${appCode}.client.dto.${modelPackName}.${className}DTO;
import cn.focusmedia.central.${appCode}.client.dto.${modelPackName}.${className}DeleteDTO;
import cn.focusmedia.central.${appCode}.client.dto.${modelPackName}.${className}QueryDTO;
import cn.focusmedia.central.${appCode}.client.dto.${modelPackName}.${className}UpdateDTO;
import cn.focusmedia.central.${appCode}.domain.${modelPackName}.domainobject.${className};
import cn.focusmedia.central.${appCode}.domain.${modelPackName}.domainobject.query.${className}Query;
import cn.focusmedia.central.${appCode}.domain.${modelPackName}.service.${className}DomainService;
import cn.focusmedia.fdf.common.dto.MultiResponse;
import cn.focusmedia.fdf.common.dto.SingleResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* ${className}DomainService
*
* @author ${author}
* @since ${date}
**/
@Api(tags = "${objectName}")
@Slf4j
@RestController
@RequestMapping("/v1/api/${objectName}")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${className}Service {

    private final ${className}DomainService ${objectName}DomainService;

    private final ${className}DTOConvert ${objectName}DTOConvert;

    @ApiOperation(value = "查询", notes = "查询")
    @PostMapping(value = "/query")
    public MultiResponse<${className}DTO> query(@RequestBody @Valid ${className}QueryDTO param) {
        ${className}Query query = ${objectName}DTOConvert.convertDTO2Query(param);
        MultiResponse<${className}> result = ${objectName}DomainService.query(query);
        return MultiResponse.of(${objectName}DTOConvert.domainList2DTOList(result.getData()), result.getTotal());
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public SingleResponse<${className}DTO> add(@RequestBody @Valid ${className}AddDTO param) {

        SingleResponse<${className}> result = ${objectName}DomainService.add(param);
        if (result.isSuccess()) {
            return SingleResponse.of(${objectName}DTOConvert.domain2DTO(result.getData()));
        } else {
            return SingleResponse.buildFailure(result.getErrCode(), result.getErrMessage());
        }

    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping(value = "/update")
    public SingleResponse<${className}DTO> update(@RequestBody @Valid ${className}UpdateDTO param) {

        SingleResponse<${className}> result = ${objectName}DomainService.update(param);
        if (result.isSuccess()) {
            return SingleResponse.of(${objectName}DTOConvert.domain2DTO(result.getData()));
        } else {
            return SingleResponse.buildFailure(result.getErrCode(), result.getErrMessage());
        }

    }

    @ApiOperation(value = "删除(逻辑删)", notes = "删除(逻辑删)")
    @PostMapping(value = "/delete")
    public SingleResponse<${className}DTO> delete(@RequestBody @Valid ${className}DeleteDTO param) {

        SingleResponse<${className}> result = ${objectName}DomainService.delete(param);
        if (result.isSuccess()) {
            return SingleResponse.of(${objectName}DTOConvert.domain2DTO(result.getData()));
        } else {
            return SingleResponse.buildFailure(result.getErrCode(), result.getErrMessage());
        }

    }

}